package com.wwwarehouse.contract.orders.place_order;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.bean.ObligationItemBean;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;


/**
 * 义务项Fragment
 * Created by ShineF on 2017/10/24 0024.
 */

public class ObligationItemFragment extends BaseFragment implements View.OnClickListener {

    private TextView mTvOrderDiscount;
    private TextView mTvArrivalTime;
    private Button mBtnConfirm;
    private StateLayout mStateLayout;
    private Long mContractUkid;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_obligation, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mTvArrivalTime = $(R.id.tv_arrival_time);
        mTvOrderDiscount = $(R.id.tv_order_discount);
        mBtnConfirm = $(R.id.btn_confirm);
        mStateLayout = $(R.id.sl_state);
        mStateLayout.showProgressView(true);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ObligationItemFragment) {
            mActivity.setTitle(R.string.obligation_item);
        }
    }

    @Override
    public void requestDatas() {
        mBtnConfirm.setOnClickListener(this);
        mStateLayout.setEmptyListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                requestHttp();
            }
        });
        Bundle bundle = getArguments();
        if (bundle != null) {
            mContractUkid = bundle.getLong("contractUkid");
            requestHttp();
        }
    }

    public void requestHttp() {
        Map<String, Object> map = new HashMap<>();
        map.put("contractUkid", mContractUkid);
        loadData(ContractConstant.LOAD_OBLIGATION, map, 1);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_confirm) {
            popFragment();
        }
    }


    public void loadData(String url, Map map, int what) {
        NoHttpUtils.httpPost(url, map, new NoHttpUtils.OnResponseListener() {
            @Override
            public void onSucceed(CommonClass commonClass, int i) {
                if (commonClass.getCode().equals("0")) {
                    List<ObligationItemBean> dataList = JSON.parseArray(commonClass.getData().toString(), ObligationItemBean.class);
                    if (commonClass.getData() == null || dataList.size() == 0) {
                        mStateLayout.showEmptyView(getString(R.string.not_setting), true);
                    } else {
                        setData(dataList);
                    }
                }
            }

            @Override
            public void onFailed(String s, int i) {

            }

            @Override
            public void onFinish(int i) {

            }
        }, what);
    }

    private void setData(List<ObligationItemBean> dataList) {
        ObligationItemBean dataBean = dataList.get(0);
        if (dataBean.getDefaultType().equals("0")) {
            mTvArrivalTime.setText(StringUtils.getResourceStr(mActivity, R.string.arrival_time, dataBean.getDefaultValueMin(), dataBean.getDefaultValueMax()));
        } else if (dataBean.getDefaultType().equals("1")) {
            mTvArrivalTime.setText(StringUtils.getResourceStr(mActivity, R.string.arrival_num, dataBean.getDefaultValueMin(), dataBean.getDefaultValueMax()));
        }
        if (dataBean.getRewandType().equals("0")) {
            mTvOrderDiscount.setText(getString(R.string.rongren));
        } else {
            mTvOrderDiscount.setText(StringUtils.getResourceStr(mActivity, R.string.order_discount, dataBean.getRewandValue()));
        }
        mStateLayout.showContentView();
    }
}
